### Examines the levels of concentration by type of lists between 1998 and 2018
### Data are mentioned in the text and graphs presented in the appendix.

v <- read.csv("~/Dropbox/Data/Paper-BLS8/Paper-BLS8-AdditionalData/Resultados/agg-1998-2018.csv")
v$dif.top2share <- v$dif.top2/v$total.votos

### Number of lists competing in each year (mentioned in text)
table(v$ANO_ELEICAO)#number of lists

### Check how many parties only fielded one candidate
table(is.na(v$dif.top2),v$categoria,v$ANO_ELEICAO)
vv <- subset(v,is.na(v$dif.top2)==F) #those that fielded more than one candidate

### Table of category by year
ycat <- table(v$categoria,v$ANO_ELEICAO)
print(ycat)

ydif <- by(v$dif.top2share,list(v$categoria,v$ANO_ELEICAO), mean,na.rm=T)
tab.diff <- matrix(as.numeric(ydif),nrow=3,ncol=6,
                   dimnames=list(rownames(ycat),colnames(ycat)))
print(tab.diff)

yhhi <- by(v$hhi,list(v$categoria,v$ANO_ELEICAO),mean,na.rm=T)
tab.hhi <- matrix(as.numeric(yhhi),nrow=3,ncol=6,
                  dimnames=list(rownames(ycat),colnames(ycat)))
print(tab.hhi)

### Table of indicators by type of party (excluding parties that fielded a single candidate)
by(vv$dif.top2share,vv$categoria,mean,na.rm=T)
by(vv$hhi,vv$categoria,mean,na.rm=T)
table(vv$categoria)

# HHI index by year for insuccesful lists (mentioned in text)
by(v$hhi[v$categoria!="Nenhum eleito"]
   ,v$ANO_ELEICAO[v$categoria!="Nenhum eleito"]
   ,mean,na.rm=T)

# Check whether OMSD are really different from larger lists #
# First establish a threshold to deem a list concentrated 
# Threshold is the .75 percentile of larger lists for each indicator 
thresh.hhi <- quantile(v$hhi[v$categoria=="Mais de um eleito"],prob=.75)
thresh.dif <- quantile(v$dif.top2share[v$categoria=="Mais de um eleito"],prob=.75,na.rm=T)

# Check share of OMSD that are more concentrated than threhsold in at least one indicator 
# Overall:
mean(v$hhi[v$categoria!="Um eleito"]>thresh.hhi|
       v$dif.top2share[v$categoria!="Um eleito"]>thresh.dif
     ,na.rm=T)

# Over time for HHI: 
by(v$hhi[v$categoria!="Um eleito"]>thresh.hhi|
     v$dif.top2share[v$categoria!="Um eleito"]>thresh.dif
   ,v$ANO_ELEICAO[v$categoria!="Um eleito"]
   ,mean,na.rm=T)


### Plot HHIs (iin Appendix)
hist(v$hhi[v$categoria=="Um eleito"],freq=F)
hist(v$hhi[v$categoria=="Mais de um eleito"],freq=F)
hist(v$hhi[v$categoria=="Nenhum eleito"],freq=F)

png("~/Dropbox/LatexFiles/Paper-BLS8/Figures/fig-HHIbytype1998-2018.png", width=6, height=4, units="in",res = 300)
par(mar=c(4,4,3,1))
plot(density(v$hhi[v$categoria=="Um eleito"])
     ,ylim=c(0,4), bty="n", xlab="HHI Concentration Index", main=""
     # ,main="Distribution of Vote Concentration by Type of Party"
)
lines(density(v$hhi[v$categoria=="Mais de um eleito"]),lty=2)
lines(density(v$hhi[v$categoria=="Nenhum eleito"]),lty=3)
abline(v=thresh.hhi)
legend(x="topright",legend=c("One Elected","Multiple Elected","None Elected"),lty=c(1,2,3),bty="n")
dev.off()



png("~/Dropbox/LatexFiles/Paper-BLS8/Figures/fig-DIFbytype1998-2018.png", width=6, height=4, units="in",res = 300)
par(mar=c(4,4,3,1))
plot(density(v$dif.top2share[v$categoria=="Um eleito"],na.rm=T)
     ,ylim=c(0,5), bty="n", xlab="Relative Difference Between 1st and 2nd in Party", main=""
     # ,main="Distribution of Vote Concentration by Type of Party"
)
lines(density(v$dif.top2share[v$categoria=="Mais de um eleito"],na.rm=T),lty=2)
lines(density(v$dif.top2share[v$categoria=="Nenhum eleito"],na.rm=T),lty=3)
abline(v=thresh.dif)
legend(x="topright",legend=c("One Elected","Multiple Elected","None Elected"),lty=c(1,2,3),bty="n")
dev.off()


